local Class = require('lua.lang.Class')
local LexState = require('lua.vm.compiler.LexState')
local FunctionState = require('lua.vm.compiler.FunctionState')
local LuaString = require('lua.vm.LuaString')
local Prototype = require('lua.vm.Prototype')
local Constants = require('lua.vm.compiler.Constants')

--- @class CompileState : Class
local CompileState = Class {}

--- @return CompileState
function CompileState:new()
	--- @type CompileState
	local o = {}
	return setmetatable(o, CompileState)
end

--- @param z InputStream
--- @param name string
--- @return Prototype
function CompileState:luaYParseChunk(z, name)
	local lexState = LexState:new(z)
	local funcState = FunctionState:new()
	lexState.fs = funcState
	lexState:setInput(z:read(), z, LuaString:valueOf(name))
	funcState.f = Prototype:new()
	funcState.f.source = LuaString:valueOf(name)
	lexState:mainFunc(funcState)
	Constants:assert(funcState.prev == nil)
	Constants:assert(lexState.dyd == nil or (lexState.dyd.activeVariableCount == 0 and
			lexState.dyd.gotoLabelCount == 0 and lexState.dyd.labelCount == 0))
	return funcState.f
end

return CompileState